const express=require('express');
const admin=express.Router();
const axios = require("axios");
// 登陆页面
const jwt = require("jsonwebtoken")
const users = require("./admin/users");
const faceTitle = require("./admin/faceTitle");
const penTitle = require("./admin/penTitle");
const index = require("./admin/index"); 
const tables = require("./admin/tables"); 
const interview = require("./admin/interview"); 
// const session = require('express-session');usd                           
admin.get('/login',(req, res)=>{
    // if(req.session.token&&req.session.token!=null){
        // for(let i=0;i<req.app.locals.users.length ;i++){
        //     if(req.app.locals.users[i]==jwt.decode(req.session.token).id){
               
        //         req.session.token=null
        //         res.redirect('/admin/login');
        //         console.log("++++++"+req.app.locals.users);
        //     }
        // }
        // req.app.locals.users.splice( req.app.locals.users.indexOf(jwt.decode(req.session.token).id),1)
    // }
    if(req.session.token){
        req.session.token=null;
    }
        res.render('admin/login');
        // console.log("loginOut:"+req.app.locals.users);
    
});
admin.use('/users',users)
admin.use('/faceTitle',faceTitle)
admin.use('/penTitle',penTitle)
admin.use('/index',index)
admin.use('/tables',tables)
admin.use('/interview',interview)
// =======
admin.get('/addFaceTitle',(req, res)=>{res.render('admin/faceTitle_item');});
//上传简历
admin.get('/upload',(req, res)=>{res.render('admin/upload',({userInfo:jwt.decode(req.session.token)}));});

// 用户管理
// admin.get('/users',require('./admin/users'));
// const users = require("./admin/users");
// const index = require('./home/index')
const session = require('express-session');
// admin.use('/users',users)
// >>>>>>> 0c4879d192219517661ac80df6bc8dc14edcff0b
// admin.use('/index', index)


admin.use(express.urlencoded({ extended: false }));
admin.use(express.json());

//上传简历
let upload = require("./admin/upload");
admin.use('/upload',upload)





// 请求拦截
admin.post('/login',require("./admin/login"));
// 表格导出
admin.get('/tables',(req, res)=>{res.render('admin/tables');});
// 退出
admin.get('/loginOut',(req, res)=>{
    // console.log(req.app.locals.users);
    const {_id} = req.query
    // console.log(_id);
    // req.session.token=null
    Clear.Session();
    // console.log(req.session.token);
    req.app.locals.users.splice( req.app.locals.users.indexOf(_id,1))
    res.redirect('/admin/login');
    // for(let i=0;i<req.app.locals.users.length ;i++){
    //     if(req.app.locals.users[i]==_id){
            
    //         console.log(req.app.locals.users);
    //     }
    // }
    
      
});
module.exports=admin;